<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Orders.php");							//--> Orders
include_once("Operation.php");						//--> Operation
include_once("TimeZone.php");						//--> TimeZone
require_once("controlHeader.php");					//--> system control header

$objOperate 	= new Operation();							//--> Operation
$objOrders		= new Orders(DB_TAG_PUBLIC, $uid);			//--> Orders
$oTimeZone 		= new TimeZone();							//--> TimeZone


$error_message 	= '';

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);


$arrOrders = $objOrders->getByID($arrOperate, $id);

if(isset($_POST['Submit'])){
	$id 			= $_POST['id'];
	$status 		= $_POST['status'];
	$comments 		= $_POST['comments'];
	$notified		= $_POST['notify'];

	if(empty($notified))
		$notified = 'Yes';
	else
		$notified = 'No';
	
	if(empty($comments))
		$error_message = 'The comments should\'t be empty.';
	
	if(empty($error_message)){
		$isReturn = $objOrders->addStatus($arrOperate, $id, $status, $notified, $comments);
		if($isReturn){
			$objOrders->update(DB_TAG_PUBLIC, "UPDATE orders SET status = '$status' WHERE id = $id");
			
			if($status == 'shipped'){
				$customer 	= $objOrders->getRow(DB_TAG_SYSTEM, "SELECT id, firstname, lastname, email FROM customers WHERE id = " . $arrOrders['customers_id']);
				
				if(count($customer) > 0){
					$emailContent = "<div><p>Dear " . $customer['firstname'] . ", <br />
  <br />
  Thank you for shopping at 123gohelmets.com. <br />
  <br />
  <br />
  Your order is packaged and ready to leave our Distribution Center. <br />
  <br />
  Your order number is  (#" . $arrOrders['id'] . ") and will be shipped via UNITED PARCEL SERVICE COMM GRNDTRA. <br />
  Within 24-48 hours of receiving this email, you may track the delivery status of your order at: </p>
<p>UPS tracking #(                              ) </p>
<p>Please note: We have assigned a tracking number to your package(s) prior to the carrier's receipt of your order. If your tracking number does not appear immediately, the carrier may still be processing this information.  You may need to check the carrier's site periodically for updated information. <br />
  <br />
  This message is electronically generated, please do not respond to this email. If you need further assistance please e-mail us at 123gohelmets@mail.com. <br />
  <br />
  Thanks again for choosing 123gohelmets.com. We look forward to serving your future Helmet and Goggles needs. </p>
					</div>";
				
				
					$subject	= 'Thank you for shopping at 123gohelmets.com.';
					
					/* To send HTML mail, you can set the Content-type header. */
					$headers  = "MIME-Version: 1.0\r\n";
					$headers .= "Content-type: text/html; charset=utf8\r\n";
					
					/* additional headers */
					$headers .= "From: Sam <atvglove@gmail.com>\r\n";
					
					/* and now mail it */
					$siSuccess = mail($customer['email'], $subject, $emailContent, $headers);
					
					if($siSuccess){
				    	$error_message = 'Notice email send to ' . $customer['email'] . '<br>';
					}else
				    	$error_message = 'Notice email send to ' . $customer['email'] . ' failed. <br>';
				}
			}
			
			$error_message .= 'edit successfully.';
		}else{
			$error_message = 'edit failure.';
		}
	}
}

$arrStatusList = array();

$arrDataList 	= $objOrders->listCustom(DB_TAG_PUBLIC, '*', "orders_products WHERE ordersID=$id", '', 0, 100, '');
$arrAccountList = $objOrders->listCustom(DB_TAG_PUBLIC, '*', "orders_account WHERE ordersID=$id", " ORDER BY classes", 0, 100, '');
$arrAllStatusList 	= $objOrders->listCustom(DB_TAG_PUBLIC, '*', "orders_status_history WHERE ordersID=$id", " ORDER BY id", 0, 100, '');
//$arrAttributes 	= $objOrders->listCustom(DB_TAG_PUBLIC, '*', "orders_products_attributes WHERE ordersID=$id", " ORDER BY id", 0, 100, '');
foreach($arrAllStatusList as $row){
	$DatePurchased = $oTimeZone->getTime($row['createdTime'], 'UTC', 'America/Los_Angeles');
	
	$row['datePurchased'] = $DatePurchased;
	
	$arrStatusList[] = $row;
}

$arrOrdersProducts	= array();
foreach($arrDataList as $key => $row){
	$arrAttribute = $objOrders->getAllData(DB_TAG_PUBLIC, "SELECT * FROM orders_products_attributes WHERE orderProdID = ".$row['id']." AND ordersID = $id");
	if(count($arrAttribute) > 0){
		$row['attributes'] 	= $arrAttribute;
	}
	
	$arrOrdersProducts[] = $row;
}

/*----- out html -----*/
$smarty = new Smarty();	//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = CACHE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);

$smarty->assign('id', $id);

$smarty->assign('arrOrders', $arrOrders);
$smarty->assign('arrDataList', $arrOrdersProducts);
$smarty->assign('arrAccountList', $arrAccountList);
$smarty->assign('arrStatusList', $arrStatusList);

$smarty->display('listOrders_View.htm');
?>
